package com.phiture.erp.produce.dal.mapper;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.phiture.erp.produce.dal.entity.ErpProduceProcessDO;
import com.phiture.erp.produce.dal.entity.ErpProduceRoutingDO;
import com.phiture.erp.produce.dal.entity.ErpProduceRoutingDetailDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 工艺路线明细 Mapper
 *
 * @author lekaihuai
 */
@Mapper
public interface ErpProduceRoutingDetailMapper extends BaseMapperX<ErpProduceRoutingDetailDO> {

    default List<ErpProduceRoutingDetailDO> selectListByRoutingId(Long routingId) {
        return selectList(ErpProduceRoutingDetailDO::getRoutingId, routingId);
    }

    default List<ErpProduceProcessDO> selectListByMaterialId(Long materialId) {
        MPJLambdaWrapperX<ErpProduceRoutingDetailDO> wrapper = new MPJLambdaWrapperX<>();
        wrapper.leftJoin(ErpProduceRoutingDO.class, ErpProduceRoutingDO::getId, ErpProduceRoutingDetailDO::getRoutingId)
                .leftJoin(ErpProduceProcessDO.class, ErpProduceProcessDO::getId, ErpProduceRoutingDetailDO::getProcessId)
                .eq(ErpProduceRoutingDO::getMaterialId, materialId)
                .selectAs(ErpProduceProcessDO::getProcessName, "processName")
                .selectAs(ErpProduceProcessDO::getCodeNo, "codeNo")
                .selectAs(ErpProduceProcessDO::getId, "id")
                .selectAs(ErpProduceProcessDO::getWorkCenter, "workCenter");

        List<Map<String, Object>> maps = selectMaps(wrapper);
        List<ErpProduceProcessDO> result = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            ErpProduceProcessDO processDO = new ErpProduceProcessDO();
            // 假设 ErpProduceRoutingDetailDO 有对应的 set 方法来设置属性
            if (map.get("processName") != null) {
                processDO.setProcessName((String)map.get("processName"));
            }
            if (map.get("codeNo") != null) {
                processDO.setCodeNo((String) map.get("codeNo"));
            }
            if (map.get("id") != null) {
                processDO.setId((Long) map.get("id"));
            }
            if (map.get("workCenter") != null) {
                processDO.setWorkCenter((Long) map.get("workCenter"));
            }
            result.add(processDO);
        }
        return result;
    }


    default int deleteByRoutingId(Long routingId) {
        return delete(ErpProduceRoutingDetailDO::getRoutingId, routingId);
    }

}
